package com.isoft.controller;

import com.alibaba.excel.EasyExcel;
import com.isoft.mapper.StudentMapper;
import com.isoft.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.ConditionalOnDefaultWebSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
//李四修改
//ccccc
//功能修改
@Controller
public class StudentController {
    @Autowired
    StudentMapper studentMapper;

    @GetMapping("/toExport")
    public String toExport() {
        return "export";
    }

    @RequestMapping("/exportDBToExcel")
    public ModelAndView exportDBToExcel(HttpServletResponse rep, ModelAndView view) {
        try {
            String filename = null;
            rep.setContentType("application/vnd.ms-excel");
            rep.setCharacterEncoding("utf-8");
            filename = URLEncoder.encode("studentdb.xlsx", "utf-8");
            rep.setHeader("Content-disposition", "attachment;filename=" + filename);
            List<Student> list = studentMapper.findAll();
            EasyExcel.write(rep.getOutputStream(), Student.class).sheet("学生信息表").doWrite(list);
            view.setViewName("export");
            view.addObject("msg", "导出数据成功!");
        } catch (Exception e) {
            System.out.println("导出数据失败");
            view.addObject("msg", "导出数据失败!");
            e.printStackTrace();
        }
        return view;
    }
}
